﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

#include "WorkFlowBase.h"


namespace gmepgcs
{
    class GCS_SERVICE_EXPORTS BatchWorkflowBase : public WorkflowBase
    {
        Q_OBJECT

    public:
        explicit BatchWorkflowBase(const int &totalCount, QObject *parent = nullptr);
        virtual ~BatchWorkflowBase();
        
    public:
        int GetSuccessCount() const;
        int GetFaliCount() const;

    public:
        template<typename Func>
        BatchWorkflowBase *OnPhase(Func handler);

    signals:
        void Phased(const int index);

    protected:
        virtual void InternalProceed() override final;
        virtual WorkflowBase *GetWorkflow(const int &index) = 0;

    private:
        void Execute(const int index = 0);
        void MoveNext(const int current, const bool success, const std::wstring message);

    private:
        int m_total;
        int m_successCount;
        int m_failCount;
    };


    template<typename Func>
    inline BatchWorkflowBase * BatchWorkflowBase::OnPhase(Func handler)
    {
        connect(this, &BatchWorkflowBase::Phased, [=](const int index) {
            handler(index);
        });

        return this;
    }
}